package com.paireach.spanablestring01;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 实际中发现 INCLUSIVE  EXCLUSIVE 不起作用，都是包左不包右
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.tv7)
    TextView tv7;
    @BindView(R.id.tv8)
    TextView tv8;
    @BindView(R.id.tv9)
    TextView tv9;
    @BindView(R.id.tv10)
    TextView tv10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initTV1();//ForegroundColorSpan  BackgroundColorSpan
        initTV2();//  BackgroundColorSpan  背景色
        initTV3();//  RelativeSizeSpan      字体大小
        initTV4();//  StrikethroughSpan     删除线
        initTV5();//  UnderlineSpan        下划线
        initTV6();//  SuperscriptSpan   上角标
        initTV7();//  SubscriptSpan     下角标
        initTV8();//  StyleSpan         字体类型
        initTV9();//  ImageSpan         图片
        initTV10();//  URLSpan   超链接
    }

    private void initTV1() {
        SpannableString text = new SpannableString("设置文字的前景色为红色");
        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#FF00FF"));
        text.setSpan(span, 2, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        tv1.setText(text);
    }

    private void initTV2() {
        SpannableString text = new SpannableString("设置文字的背景色为红色");
        BackgroundColorSpan span = new BackgroundColorSpan(Color.RED);
        text.setSpan(span, 2, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        tv2.setText(text);
    }

    private void initTV3() {
        SpannableString text = new SpannableString("设置文字的文字大小");
        RelativeSizeSpan span = new RelativeSizeSpan(2.0f);
        AbsoluteSizeSpan aSpan = new AbsoluteSizeSpan(50);
        text.setSpan(span, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(aSpan, 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv3.setText(text);
    }

    private void initTV4() {
        SpannableString text = new SpannableString("设置文字的删除线");
        StrikethroughSpan span = new StrikethroughSpan();
        text.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv4.setText(text);
    }

    private void initTV5() {
        SpannableString text = new SpannableString("设置文字的下划线");
        UnderlineSpan span = new UnderlineSpan();
        text.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv5.setText(text);
    }

    private void initTV6() {
        SpannableString text = new SpannableString("设置文字的上角标");
        SuperscriptSpan span = new SuperscriptSpan();
        text.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv6.setText(text);
    }

    private void initTV7() {
        SpannableString text = new SpannableString("设置文字的下角标");
        SubscriptSpan span = new SubscriptSpan();
        text.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv7.setText(text);
    }

    private void initTV8() {
        SpannableString text = new SpannableString("设置文字的字体格式加粗斜体等");
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        StyleSpan italicSpan = new StyleSpan(Typeface.ITALIC);
        text.setSpan(boldSpan, 9, 11, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        text.setSpan(italicSpan, 11, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv8.setText(text);
    }

    private void initTV9() {
        SpannableString text = new SpannableString("在文本中添加表情(表情)");
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        drawable.setBounds(0, 0, 50, 50);
        ImageSpan span = new ImageSpan(drawable);
        text.setSpan(span, 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv9.setText(text);
    }

    private void initTV10() {
        SpannableString text = new SpannableString("给文字设置超链接百度");
        URLSpan span = new URLSpan("http://www.baidu.com/");
        text.setSpan(span, 8, 9, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv10.setMovementMethod(LinkMovementMethod.getInstance());//跳转到手机默认浏览器中，必须加这一行代码
        tv10.setHighlightColor(Color.parseColor("#969696"));
        tv10.setText(text);
    }



}
